/**
 * 主要是为了查询数据库server相关参数和一些监控需求。
 * 先处理MongoDB，后期再加入Mysql或者其他数据库服务.
 * 
 */

const utils = require('../libs/utils.js');
const BaseService = require('../libs/baseService.js');
const child_process = require('child_process');

let service = new BaseService();

function execShellAsync(cmd) {
    return new Promise(function(resolve, reject){
        child_process.exec(cmd, {maxBuffer: 10240*1024}, function(e, stdout, stderr){
            log(cmd);
            log('stdout:' + stdout);
            log('stderr:' + stderr);
            if(e){
                log('e' + e.stack || e);
                return reject(e);
            }
            var result = stdout ? stdout : stderr;
            resolve(result);
        });
    });
}

service.showDBSizeInfo = async (ctx, next) => {
    try {
        await execShellAsync('use HuaXi');
        const db_info = await execShellAsync('db.stats();');

        service.backSuccess(ctx, db_info);
    } catch (e) {
        console.error(e || e.message);
        service.backError(ctx, 500 , e.message);
    }
}

